package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.account.AccountFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderFeignClient;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
@SuppressWarnings({"all"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private AccountFeignClient accountFeignClient;
    @Autowired
    private OrderFeignClient orderFeignClient;

    @Override
    public PaymentInfo getPaymentInfoByOutradeNo(String outTradeNo) {

        return paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOutTradeNo,outTradeNo));
    }

    @Override
    public void updatePaymentInfo(Transaction transaction) {
        Integer total = transaction.getAmount().getTotal();
        Transaction.TradeStateEnum tradeState = transaction.getTradeState();
        PaymentInfo paymentInfo = getPaymentInfoByOutradeNo(transaction.getOutTradeNo());
        if (Transaction.TradeStateEnum.SUCCESS.equals(tradeState)){
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
            paymentInfo.setCallbackContent("支付成功");

        }else {paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
            paymentInfo.setCallbackContent("支付失败");

        }
        paymentInfo.setAmount(new BigDecimal(total));
        paymentInfo.setOutTradeNo(transaction.getOutTradeNo());
        paymentInfo.setCallbackTime(new Date());
        paymentInfoMapper.updateById(paymentInfo);


        //判断支付类型
        String paymentType = paymentInfo.getPaymentType();
        //判断订单处理
        if(SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)){
            //更新订单状态
            Result result=orderFeignClient.orderPaySuccess(transaction.getOutTradeNo());
            //判断是否成功
            if(result.getCode()!=200){

                throw new GuiguException(500,"更新订单状态异常");

            }

        }

        //充值处理
        if(SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentType)){
            //更新充值状态
            Result result=accountFeignClient.rechargePaySuccess(transaction.getOutTradeNo());
            //判断是否成功
            if(result.getCode()!=200){

                throw new GuiguException(500,"更新充值状态异常");

            }

        }
    }
}
